// MSimplePlot.cpp: Implementierung der Klasse MSimplePlot.
//
//////////////////////////////////////////////////////////////////////

#include "MSimplePlot.h"
//#include "MiriamColors.h"
//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
///constructor
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MSimplePlot::MSimplePlot(int dataLength, QWidget *parent): QLabel(parent)
{
    m_numElements = dataLength;
    setAutoFillBackground ( true );
	setFrameStyle (QFrame::StyledPanel);
    
	xdata.setSize(dataLength);
	ydata.setSize(dataLength);
	pixels.resize(dataLength);

	m_showTexts = true;
	m_xAxisText = "x";
	m_yAxisText = "y";
    
    m_textColor = QColor(Qt::yellow);
}

MSimplePlot::~MSimplePlot()
{

}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
///	paintEvent(QPaintEvent *)
///this is where the data gets plotted
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void MSimplePlot::paintEvent(QPaintEvent *event)
{
	QLabel::paintEvent(event);

	QPainter painter(this);
    painter.setPen(QPen(m_textColor));
	painter.setRenderHint(QPainter::Antialiasing, true);

	painter.drawPolyline(pixels.data(), pixels.size());

	if(m_showTexts){
		double xScale = xdata.maximum()-xdata.minimum();
		QString xScaleText(QString("%1 %2").arg(xScale, 1, 'f', 1).arg(m_xAxisText));
		painter.drawText(QRect(0,0,width(), height()),  Qt::AlignRight|Qt::AlignBottom, xScaleText );

		double yScale = ydata.maximum()-ydata.minimum();
		QString yScaleText( QString("%1 %2").arg(yScale, 1, 'f', 1).arg(m_yAxisText));
		painter.drawText(QRect(0,0,width(), height()),  Qt::AlignLeft|Qt::AlignTop, yScaleText );
	/**/
	//	painter.drawText(QPointF(0,0), QString("1min") );
	//	painter.drawText(QPoint(0,0), QString("1min"));//why doesn't this work??!
	}
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
///	void update (double y, double x)
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void MSimplePlot::update (double y, double x)
{
	ydata.enqueue(y);
	if(x)
		xdata.enqueue(x);
	else
		xdata.enqueue(xdata.maximum()+1);//something like an increment

	pixels.clear();

	//scale all the data again
	double xmax = xdata.maximum();
	double xmin = xdata.minimum();
	double ymax = ydata.maximum();
	double ymin = ydata.minimum();

	if(xmax-xmin < 1E-9)xmax+=1;
	if(ymax-ymin < 1E-9)ymax+=1;

	for(int i=0; i<xdata.size(); ++i){

		int xpix = (int)(width()*(xdata.at(i)-xmin)/(xmax-xmin));
		int ypix = (int)(height() - height()*(ydata.at(i)-ymin)/(ymax-ymin));
		pixels.append (QPointF(xpix, ypix));
	}

	QLabel::update();//so that a paint event is called everytime there's new data
}


/*!
    \fn MSimplePlot::textColor() const
 */
const QColor & MSimplePlot::textColor() const
{
    return m_textColor;
}


/*!
    \fn MSimplePlot::setTextColor(QColor textColor)
 */
void MSimplePlot::setTextColor(QColor textColor)
{
    m_textColor = textColor;
}


/*!
    \fn MSimplePlot::xLabel()
 */
const QString & MSimplePlot::xLabel() const
{
    return m_xAxisText;
}


/*!
    \fn MSimplePlot::yLabel()
 */
const QString & MSimplePlot::yLabel() const
{
    return m_yAxisText;
}


/*!
    \fn MSimplePlot::setXlabel(QString label)
 */
void MSimplePlot::setXlabel(QString label)
{
    m_xAxisText = label;
}


/*!
    \fn MSimplePlot::setYlabel(QString label)
 */
void MSimplePlot::setYlabel(QString label)
{
    m_yAxisText = label;
}



/*!
    \fn MSimplePlot::numElements() const
 */
const int MSimplePlot::numElements() const
{
    return m_numElements;
}


/*!
    \fn MSimplePlot::setNumElements(int elements)
 */
void MSimplePlot::setNumElements(int elements)
{
    xdata.setSize(elements);
    ydata.setSize(elements);
    pixels.resize(elements);
    m_numElements = xdata.size();
}
